/*
* pps_bom
*/
package com.pitop.assembly.ctrl;


import com.alibaba.fastjson.JSON;
import com.pitop.assembly.entity.AssemblyMaterial;
import com.pitop.assembly.service.AssemblyMaterialSVImpl;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.redis.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 构成组件的物料 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/assemblyMaterial")
@Slf4j
@Api(value = "构成组件的物料控制器", description = "构成组件的物料控制器")
public class AssemblyMaterialCtrl {

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private AssemblyMaterialSVImpl assemblyMaterialSVImpl;


    /**
     * 查询AssemblyMaterial一个详情信息
     *
     * @param id
     * @param assemblyCode 组件编码
     * @param materialCode 物料编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询AssemblyMaterial一个详情信息", notes = "查询AssemblyMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "assemblyCode", value = "组件编码", dataType = "java.lang.String", paramType = "query"),
            @ApiImplicitParam(name = "materialCode", value = "物料编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String assemblyCode, String materialCode) {
        if (id == null) {
            return null;
        }
        if (assemblyCode == null) {
            return null;
        }
        if (materialCode == null) {
            return null;
        }
        AssemblyMaterial assemblyMaterial = assemblyMaterialSVImpl.load(id, assemblyCode, materialCode);
        log.info(JSON.toJSONString(assemblyMaterial));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyMaterial);
    }

    /**
     * 查询AssemblyMaterial一个详情信息
     *
     * @param id
     * @param assemblyCode 组件编码
     * @param materialCode 物料编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询AssemblyMaterial一个详情信息", notes = "查询AssemblyMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "assemblyCode", value = "组件编码", dataType = "java.lang.String", paramType = "query"),
            @ApiImplicitParam(name = "materialCode", value = "物料编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/get")
    @ResponseBody
    public BeanRet get(Long id, String assemblyCode, String materialCode) {
        if (id == null) {
            return null;
        }
        if (assemblyCode == null) {
            return null;
        }
        if (materialCode == null) {
            return null;
        }
        AssemblyMaterial assemblyMaterial = assemblyMaterialSVImpl.get(id, assemblyCode, materialCode);
        log.info(JSON.toJSONString(assemblyMaterial));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyMaterial);
    }


    /**
     * 根据条件id查询AssemblyMaterial一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询AssemblyMaterial一个详情信息", notes = "查询AssemblyMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        AssemblyMaterial assemblyMaterial = assemblyMaterialSVImpl.loadById(id);
        log.info(JSON.toJSONString(assemblyMaterial));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyMaterial);
    }

    /**
     * 根据条件assemblyCode查询AssemblyMaterial一个详情信息
     *
     * @param assemblyCode 组件编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询AssemblyMaterial一个详情信息", notes = "查询AssemblyMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "assemblyCode", value = "组件编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/assemblyCode/{assemblyCode}")
    @ResponseBody
    public BeanRet loadByAssemblyCode(@PathVariable String assemblyCode) {
        if (StringUtils.isEmpty(assemblyCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        AssemblyMaterial assemblyMaterial = assemblyMaterialSVImpl.loadByAssemblyCode(assemblyCode);
        log.info(JSON.toJSONString(assemblyMaterial));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyMaterial);
    }

    /**
     * 根据条件materialCode查询AssemblyMaterial一个详情信息
     *
     * @param materialCode 物料编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询AssemblyMaterial一个详情信息", notes = "查询AssemblyMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "materialCode", value = "物料编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/materialCode/{materialCode}")
    @ResponseBody
    public BeanRet loadByMaterialCode(@PathVariable String materialCode) {
        if (StringUtils.isEmpty(materialCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        AssemblyMaterial assemblyMaterial = assemblyMaterialSVImpl.loadByMaterialCode(materialCode);
        log.info(JSON.toJSONString(assemblyMaterial));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyMaterial);
    }

    /**
     * 根据条件id查询AssemblyMaterial一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询AssemblyMaterial一个详情信息", notes = "查询AssemblyMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/get/id/{id}")
    @ResponseBody
    public BeanRet getById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        AssemblyMaterial assemblyMaterial = assemblyMaterialSVImpl.getById(id);
        log.info(JSON.toJSONString(assemblyMaterial));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyMaterial);
    }

    /**
     * 根据条件assemblyCode查询AssemblyMaterial一个详情信息
     *
     * @param assemblyCode 组件编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询AssemblyMaterial一个详情信息", notes = "查询AssemblyMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "assemblyCode", value = "组件编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/assemblyCode/{assemblyCode}")
    @ResponseBody
    public BeanRet getByAssemblyCode(@PathVariable String assemblyCode) {
        if (StringUtils.isEmpty(assemblyCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        AssemblyMaterial assemblyMaterial = assemblyMaterialSVImpl.getByAssemblyCode(assemblyCode);
        log.info(JSON.toJSONString(assemblyMaterial));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyMaterial);
    }

    /**
     * 根据条件materialCode查询AssemblyMaterial一个详情信息
     *
     * @param materialCode 物料编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询AssemblyMaterial一个详情信息", notes = "查询AssemblyMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "materialCode", value = "物料编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/materialCode/{materialCode}")
    @ResponseBody
    public BeanRet getByMaterialCode(@PathVariable String materialCode) {
        if (StringUtils.isEmpty(materialCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        AssemblyMaterial assemblyMaterial = assemblyMaterialSVImpl.getByMaterialCode(materialCode);
        log.info(JSON.toJSONString(assemblyMaterial));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyMaterial);
    }

    /**
     * 查询AssemblyMaterial信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询AssemblyMaterial信息集合", notes = "查询AssemblyMaterial信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),

    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore AssemblyMaterial assemblyMaterial, @ApiIgnore Page<AssemblyMaterial> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<AssemblyMaterial> assemblyMaterials = assemblyMaterialSVImpl.list(assemblyMaterial, page.getCurPage(), page.getPageSize());
        int total = assemblyMaterialSVImpl.count(assemblyMaterial);
        page.setTotalRow(total);
        page.setVoList(assemblyMaterials);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 查询AssemblyMaterial信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询AssemblyMaterial信息集合", notes = "查询AssemblyMaterial信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "assemblyCode", value = "组件编码", paramType = "query"),
            @ApiImplicitParam(name = "materialCode", value = "物料编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, String assemblyCode, String materialCode, @ApiIgnore Page<AssemblyMaterial> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<AssemblyMaterial> assemblyMaterials = assemblyMaterialSVImpl.list(id, assemblyCode, materialCode, page.getCurPage(), page.getPageSize());
        int total = assemblyMaterialSVImpl.count(id, assemblyCode, materialCode);
        page.setTotalRow(total);
        page.setVoList(assemblyMaterials);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 创建AssemblyMaterial
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建AssemblyMaterial", notes = "创建AssemblyMaterial")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "assemblyCode", value = "组件编码", paramType = "query"),
            @ApiImplicitParam(name = "materialCode", value = "物料编码", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore AssemblyMaterial assemblyMaterial) {
        if (StringUtils.isEmpty(assemblyMaterial.getAssemblyCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(assemblyMaterial.getMaterialCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        assemblyMaterialSVImpl.save(assemblyMaterial);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyMaterial);
    }

    /**
     * 修改AssemblyMaterial
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改AssemblyMaterial", notes = "修改AssemblyMaterial")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "assemblyCode", value = "组件编码", paramType = "query"),
            @ApiImplicitParam(name = "materialCode", value = "物料编码", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore AssemblyMaterial assemblyMaterial) {
        if (StringUtils.isEmpty(assemblyMaterial.getAssemblyCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(assemblyMaterial.getMaterialCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        assemblyMaterialSVImpl.modify(assemblyMaterial);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assemblyMaterial);
    }

    /**
     * 根据主键删除AssemblyMaterial
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除AssemblyMaterial", notes = "删除AssemblyMaterial")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "assemblyCode", value = "组件编码", paramType = "query"),
            @ApiImplicitParam(name = "materialCode", value = "物料编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String assemblyCode, String materialCode) {
        if (id == null ) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }

        assemblyMaterialSVImpl.delete(id, assemblyCode, materialCode);
        return BeanRet.create(true, "删除AssemblyMaterial成功");
    }

}
